/**
 *  EKology - An Ecosystem Simulation Framework
 *  Copyright (C) 2008  Aarón Tavío - aaron.tavio at gmail.com - ULPGC
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

package net.ekology.core.gui;

import net.ekology.core.datatypes.SignalStatusType;

import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.DefaultCellEditor;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;


/**
 * @author Aarón Tavío - aaron.tavio at gmail.com
 * @version 1.0.0 - 20081019-1700
 */
public class SignalMonitorDialog extends JDialog {
    private EKologyView oEKologyView;
    private DefaultTableModel oTableModel;
    
    /** Creates new form SignalMonitorDialog */
    public SignalMonitorDialog(EKologyView oEKologyView) {
        super(oEKologyView.getFrame(), true);
        JComboBox jCombo;
        this.oEKologyView = oEKologyView;
        
        initComponents();
        setTitle("Signal Monitor");

        jCombo = new JComboBox();
        jCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { SignalStatusType.DISABLED.toString(), SignalStatusType.ENABLED.toString(), SignalStatusType.BREAKPOINT.toString() }));
        jCombo.setSelectedIndex(0);
        jCombo.setName("signalMonitoringCombo");
        signalsTable.getColumnModel().getColumn(3).setCellEditor(new DefaultCellEditor(jCombo));
        oTableModel = (DefaultTableModel) signalsTable.getModel();
    }
    
    public void setProjectName(String sProject) {
        projectName.setText(sProject);
    }
    
    public String getProjectNotes() {
        return projectNotesTextArea.getText();
    }
    
    public void setProjectNotes(String sNotes) {
        projectNotesTextArea.setText(sNotes);
    }
    
    public String getSessionName() {
        return sessionName.getText();
    }
    
    public void setSessionName(String sSession) {
        sessionName.setText(sSession);
    }
    
    public String getSessionNotes() {
        return sessionNotesTextArea.getText();
    }
    
    public void setSessionNotes(String sNotes) {
        sessionNotesTextArea.setText(sNotes);
    }
    
    public boolean getEnableMonitoring() {
        return enableMonitoring.isSelected();
    }
    
    public void setEnableMonitoring(boolean bValue) {
        enableMonitoring.setSelected(bValue);
    }
    
    public Vector getDataVector() {
        return oTableModel.getDataVector();
    }
    
    public void setDataVector(Vector<Vector> vData) {
        Vector vRow;
        
        if (oTableModel.getColumnCount() == Vector.class.cast(vData.get(0)).size()) {
            oTableModel.setRowCount(vData.size());
            for(int i=0; i<vData.size(); i++) {
                vRow = vData.get(i);
                for(int j=0; j<vRow.size(); j++) oTableModel.setValueAt(vRow.get(j), i, j);
            }
        }
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        buttonGroup1 = new javax.swing.ButtonGroup();
        projectLabel = new javax.swing.JLabel();
        acceptButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        projectName = new javax.swing.JLabel();
        projectNotesLabel = new javax.swing.JLabel();
        projectNotesPane = new javax.swing.JScrollPane();
        projectNotesTextArea = new javax.swing.JTextArea();
        sessionLabel = new javax.swing.JLabel();
        sessionName = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        signalsTable = new javax.swing.JTable();
        signalsLabel = new javax.swing.JLabel();
        sessionNotesLabel = new javax.swing.JLabel();
        sessionNotesPane = new javax.swing.JScrollPane();
        sessionNotesTextArea = new javax.swing.JTextArea();
        enableMonitoring = new javax.swing.JCheckBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N
        setResizable(false);

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(net.ekology.EKology.class).getContext().getResourceMap(SignalMonitorDialog.class);
        projectLabel.setText(resourceMap.getString("projectLabel.text")); // NOI18N
        projectLabel.setName("projectLabel"); // NOI18N

        acceptButton.setMnemonic('a');
        acceptButton.setLabel(resourceMap.getString("acceptButton.label")); // NOI18N
        acceptButton.setName("acceptButton"); // NOI18N
        acceptButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                acceptButtonMouseClicked(evt);
            }
        });

        cancelButton.setMnemonic('c');
        cancelButton.setText(resourceMap.getString("cancelButton.text")); // NOI18N
        cancelButton.setName("cancelButton"); // NOI18N
        cancelButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                cancelButtonMouseClicked(evt);
            }
        });

        projectName.setText(resourceMap.getString("projectName.text")); // NOI18N
        projectName.setName("projectName"); // NOI18N

        projectNotesLabel.setText(resourceMap.getString("projectNotesLabel.text")); // NOI18N
        projectNotesLabel.setName("projectNotesLabel"); // NOI18N

        projectNotesPane.setName("projectNotesPane"); // NOI18N

        projectNotesTextArea.setColumns(20);
        projectNotesTextArea.setRows(5);
        projectNotesTextArea.setName("projectNotesTextArea"); // NOI18N
        projectNotesTextArea.setNextFocusableComponent(sessionName);
        projectNotesPane.setViewportView(projectNotesTextArea);

        sessionLabel.setText(resourceMap.getString("sessionLabel.text")); // NOI18N
        sessionLabel.setName("sessionLabel"); // NOI18N

        sessionName.setText(resourceMap.getString("sessionName.text")); // NOI18N
        sessionName.setName("sessionName"); // NOI18N

        jScrollPane1.setAutoscrolls(true);
        jScrollPane1.setName("jScrollPane1"); // NOI18N

        signalsTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Signal", "Type", "Interval", "Monitoring"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.Long.class, java.lang.Object.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, true, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        signalsTable.setFillsViewportHeight(true);
        signalsTable.setName("signalTable"); // NOI18N
        signalsTable.setNextFocusableComponent(acceptButton);
        signalsTable.getTableHeader().setReorderingAllowed(false);

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, enableMonitoring, org.jdesktop.beansbinding.ELProperty.create("${selected}"), signalsTable, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        jScrollPane1.setViewportView(signalsTable);

        signalsLabel.setText(resourceMap.getString("signalsLabel.text")); // NOI18N
        signalsLabel.setName("signalsLabel"); // NOI18N

        sessionNotesLabel.setText(resourceMap.getString("sessionNotesLabel.text")); // NOI18N
        sessionNotesLabel.setName("sessionNotesLabel"); // NOI18N

        sessionNotesPane.setName("sessionNotesPane"); // NOI18N

        sessionNotesTextArea.setColumns(20);
        sessionNotesTextArea.setRows(5);
        sessionNotesTextArea.setName("sessionNotesTextArea"); // NOI18N
        sessionNotesTextArea.setNextFocusableComponent(sessionName);
        sessionNotesPane.setViewportView(sessionNotesTextArea);

        enableMonitoring.setText(resourceMap.getString("enableMonitoring.text")); // NOI18N
        enableMonitoring.setName("enableMonitoring"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(projectLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(projectName))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(projectNotesLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(projectNotesPane, javax.swing.GroupLayout.DEFAULT_SIZE, 365, Short.MAX_VALUE))
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 419, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(signalsLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(enableMonitoring))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(sessionLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(sessionName, javax.swing.GroupLayout.DEFAULT_SIZE, 353, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(sessionNotesLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(sessionNotesPane, javax.swing.GroupLayout.DEFAULT_SIZE, 365, Short.MAX_VALUE))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(152, 152, 152)
                        .addComponent(acceptButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cancelButton)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(projectLabel)
                    .addComponent(projectName))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(projectNotesLabel)
                    .addComponent(projectNotesPane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(sessionLabel)
                    .addComponent(sessionName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(sessionNotesLabel)
                    .addComponent(sessionNotesPane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 22, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(signalsLabel)
                    .addComponent(enableMonitoring))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(acceptButton)
                    .addComponent(cancelButton))
                .addContainerGap())
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void acceptButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_acceptButtonMouseClicked
        this.oEKologyView.signalMonitorUpdated();
        this.setVisible(false);
    }//GEN-LAST:event_acceptButtonMouseClicked

    private void cancelButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cancelButtonMouseClicked
        setVisible(false);
        this.oEKologyView.restoreSignalMonitorDialog();
    }//GEN-LAST:event_cancelButtonMouseClicked
    

     
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton acceptButton;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton cancelButton;
    private javax.swing.JCheckBox enableMonitoring;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel projectLabel;
    private javax.swing.JLabel projectName;
    private javax.swing.JLabel projectNotesLabel;
    private javax.swing.JScrollPane projectNotesPane;
    private javax.swing.JTextArea projectNotesTextArea;
    private javax.swing.JLabel sessionLabel;
    private javax.swing.JTextField sessionName;
    private javax.swing.JLabel sessionNotesLabel;
    private javax.swing.JScrollPane sessionNotesPane;
    private javax.swing.JTextArea sessionNotesTextArea;
    private javax.swing.JLabel signalsLabel;
    private javax.swing.JTable signalsTable;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
    
}
